package com.wushi.lenovo.asia5b.app_ui.other.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.images.WImageLoader;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.bean.SkuBean;

import java.util.List;
import java.util.Map;

/**
 * Created by zhangyuncai on 2019/3/18.
 */
public class UpdateAdapter extends BaseMultiItemQuickAdapter<SkuBean, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     * @param selectMap
     */
    Map<String, Integer> selectMap;

    public UpdateAdapter(List<SkuBean> data, Map<String, Integer> selectMap) {
        super(data);
        addItemType(SkuBean.catname, R.layout.item_shopcar_catname);
        addItemType(SkuBean.skutext, R.layout.item_shopcar_skutext);
        this.selectMap = selectMap;
    }

    private int getSelectPosition(SkuBean item) {
        Integer selectPosition = selectMap.get(item.no);
        if (selectPosition == null) {
            selectPosition = 0;
        }
        return selectPosition;
    }

    @Override
    protected void convert(BaseViewHolder helper, SkuBean item) {
        int position = helper.getLayoutPosition();
        if (item.getItemType() == SkuBean.catname) {
            TextView tv_shopcar_catname = helper.getView(R.id.tv_shopcar_catname);
            tv_shopcar_catname.setText(item.catName);
        } else {
            int selectPosition = getSelectPosition(item);
            ImageView iv_shopcar_skuicon = helper.getView(R.id.iv_shopcar_skuicon);
            TextView tv_shopcar_skutext = helper.getView(R.id.tv_shopcar_skutext);
            LinearLayout ll_shopcar_vessel = helper.getView(R.id.ll_shopcar_vessel);
//            iv_shopcar_skuicon.setEnabled(item.isEnable);
//            tv_shopcar_skutext.setEnabled(item.isEnable);
//            ll_shopcar_vessel.setEnabled(item.isEnable);
            helper.itemView.setEnabled(item.isEnable);
//            if (TextUtils.isEmpty(item.imgIcon)) {
//                tv_shopcar_skutext.setVisibility(View.VISIBLE);
//                iv_shopcar_skuicon.setVisibility(View.GONE);
//
//                tv_shopcar_skutext.setText(item.skuText);
//                tv_shopcar_skutext.setSelected(selectPosition == position);
//            } else {
//                tv_shopcar_skutext.setVisibility(View.GONE);
//                iv_shopcar_skuicon.setVisibility(View.VISIBLE);
//
//                WImageLoader.displayImage(mContext, item.imgIcon, iv_shopcar_skuicon);
//                iv_shopcar_skuicon.setSelected(selectPosition == position);
//            }


            if (TextUtils.isEmpty(item.imgIcon)) {
                iv_shopcar_skuicon.setVisibility(View.GONE);
            } else {
                iv_shopcar_skuicon.setVisibility(View.VISIBLE);
                WImageLoader.displayImage(mContext, item.imgIcon, iv_shopcar_skuicon);
            }
            tv_shopcar_skutext.setText(item.skuText);
            ll_shopcar_vessel.setSelected(selectPosition == position);
        }
    }
}
